package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.ProductObjectQuery;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @RequiresPermissions("product:index")
    @PermissionName("产品信息主页")
    @RequestMapping("index")
    public String index( @ModelAttribute("qo") ProductObjectQuery qo) {
        return "product/list";
    }

    @RequiresPermissions("product:list")
    @PermissionName("产品信息查询")
    @RequestMapping("list")
    @ResponseBody
    public PageResult<Product> list( @ModelAttribute("qo") ProductObjectQuery qo) {
        System.out.println(qo.getState());
        System.out.println(qo.getDeductionstate());
        PageResult<Product> list = productService.query(qo);
        return list;
    }
    @RequestMapping("query")
    @ResponseBody
    public List<Product> query() {
        return productService.getAll();
    }


    @RequiresPermissions("product:delete")
    @PermissionName("产品信息删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                productService.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除产品信息失败");
        }
        return jsonResult;
    }



    //改变保险状态
    @RequestMapping("changState")
    @ResponseBody
    public JsonResult changState(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                productService.changState(id);
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除失败");
//            throw new RuntimeException("产品信息信息删除失败");
        }
        return jsonResult;
    }
    @RequestMapping("queryByOrganizationId")
    @ResponseBody
    public List<Product> queryByOrganizationId(Long id) {
       return productService.queryByOrganizationId(id);
    }
    //改变不计免赔状态
    @RequestMapping("deductionState")
    @ResponseBody
    public JsonResult deductionState(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                productService.deductionState(id);
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除失败");
//            throw new RuntimeException("产品信息信息删除失败");
        }
        return jsonResult;
    }



    @RequiresPermissions("product:saveOrUpdate")
    @PermissionName("产品信息创建更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Product product) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (product.getId() != null) {
                productService.update(product);
            } else {
                productService.save(product);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("保存或更新产品信息失败");
        }
        return jsonResult;
    }

    /**
     *  查询保险单关联的产品和对应机构
     * @param //保险单id
     * @return
     */
    @RequestMapping("queryProductListByBillId")
    @ResponseBody
    public List<Map> queryProductListByBillId(Long billId) {
        JsonResult jsonResult = new JsonResult();
        List<Map> list = null ;
        try {
            list = productService.queryProductListByBillId(billId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


}
